package com.sf.ams.controller;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sf.ams.biz.IDataStoreXmlAnalysisBiz;
import com.sf.ams.constant.Constant;
import com.sf.ams.entity.SystemInfo;
import com.sf.ams.entity.TreeNode;
import com.sf.ams.json.JsonFieldFilter;
import com.sf.ams.utils.TreeNodeUtil;
/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017-09-26      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
@Controller
@RequestMapping("/dataStoreAnalysis")
public class DataStoreAnalysisController {
	@Autowired
	private IDataStoreXmlAnalysisBiz dataStoreXmlAnalysisBiz;
	private Logger logger = LoggerFactory.getLogger(getClass());
	@RequestMapping("/getAllSystem")
	@ResponseBody
	public List<TreeNode> getAllSystem(){
		List<TreeNode> treeNodes = new ArrayList<>();
		TreeNode treeNode = new TreeNode("systemCode","系统");
		treeNode.setChildren(TreeNodeUtil.transferSystem(dataStoreXmlAnalysisBiz.analysisDataStore()));
		treeNodes.add(treeNode);
		return treeNodes;
	}
	@RequestMapping("/getSystemDetail")
	@JsonFieldFilter(type = SystemInfo.class,exclude = "gitPassword")
	public SystemInfo getSystemDetail(String systemCode){
		return dataStoreXmlAnalysisBiz.getSystemBySystemCode(systemCode,false);
	}
	
	@RequestMapping("/downloadUserManual")
	public void downloadUserManual(HttpServletResponse response) throws UnsupportedEncodingException{
		String fileName = Constant.USER_MANUAL;
		response.setHeader("content-type", "application/octet-stream");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(Constant.USER_MANUAL_FILE_NAME, "utf-8"));
		byte [] bytes = new byte[1024];
		BufferedInputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			inputStream = new BufferedInputStream(new ClassPathResource(fileName).getInputStream());
			 int i = inputStream.read(bytes);
			while(i != -1){
				outputStream.write(bytes, 0, i);
				outputStream.flush();
				i = inputStream.read(bytes);
			}
		} catch (IOException e) {
			logger.error("下载文件失败！");
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					logger.error("close inputStream fail",e);
				}
			}
			if (outputStream != null){
				try {
					outputStream.close();
				} catch (IOException e) {
					logger.error("close inputStream fail",e);
				}
			}
		}
	}
}
